import { ref, onUnmounted } from 'vue';
import { ElMessage } from 'element-plus';

export function useSpeech() {
    // Speech Recognition (Voice to Text)
    const isRecording = ref(false);
    const recognitionText = ref('');
    const recognitionError = ref('');

    const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    let recognition = null;

    if (SpeechRecognition) {
        recognition = new SpeechRecognition();
        recognition.continuous = false;
        recognition.lang = 'zh-CN';
        recognition.interimResults = true;

        recognition.onstart = () => {
            isRecording.value = true;
            recognitionText.value = '';
            recognitionError.value = '';
        };

        recognition.onresult = (event) => {
            let interim_transcript = '';
            for (let i = event.resultIndex; i < event.results.length; ++i) {
                if (event.results[i].isFinal) {
                    recognitionText.value += event.results[i][0].transcript;
                } else {
                    interim_transcript += event.results[i][0].transcript;
                }
            }
             // You can display interim results if needed
        };

        recognition.onerror = (event) => {
            recognitionError.value = `语音识别错误: ${event.error}`;
            ElMessage.error(recognitionError.value);
            isRecording.value = false;
        };

        recognition.onend = () => {
            isRecording.value = false;
        };
    }

    const startRecording = () => {
        if (!SpeechRecognition) {
            ElMessage.warning('您的浏览器不支持语音识别。');
            return;
        }
        if (isRecording.value) {
            recognition.stop();
        } else {
            recognition.start();
        }
    };

    // Speech Synthesis (Text to Speech)
    const isSpeaking = ref(false);
    const synthesis = window.speechSynthesis;
    let utterance = null;

    const speakText = (text) => {
        if (!synthesis) {
            ElMessage.warning('您的浏览器不支持语音播报。');
            return;
        }
        if (isSpeaking.value) {
            synthesis.cancel();
        }
        
        utterance = new SpeechSynthesisUtterance(text);
        utterance.lang = 'zh-CN';
        utterance.onstart = () => isSpeaking.value = true;
        utterance.onend = () => isSpeaking.value = false;
        utterance.onerror = (e) => {
            ElMessage.error(`语音播报失败: ${e.error}`);
            isSpeaking.value = false;
        };
        synthesis.speak(utterance);
    };

    const stopSpeaking = () => {
        if (synthesis && isSpeaking.value) {
            synthesis.cancel();
        }
    };
    
    onUnmounted(() => {
        if (recognition) {
            recognition.stop();
        }
        stopSpeaking();
    });

    return {
        // Speech Recognition
        isRecording,
        recognitionText,
        recognitionError,
        startRecording,
        // Speech Synthesis
        isSpeaking,
        speakText,
        stopSpeaking,
    };
} 